﻿using System.Windows;
using System.Windows.Controls;

namespace DependencyPropertySample
{
    public class TextboxHelper
    {
        #region Attached Property 简单示例
        // 使用 propa 快速创建一个 Attached Property
        public static string GetTitle(DependencyObject obj)
        {
            return (string)obj.GetValue(TitleProperty);
        }

        public static void SetTitle(DependencyObject obj, string value)
        {
            obj.SetValue(TitleProperty, value);
        }

        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.RegisterAttached(name: "Title",
                                                propertyType: typeof(string),
                                                ownerType: typeof(TextboxHelper),
                                                defaultMetadata: new PropertyMetadata(string.Empty));
        #endregion

        #region HasText 示例

        public static bool GetHasText(DependencyObject obj)
        {
            return (bool)obj.GetValue(HasTextProperty);
        }

        public static void SetHasText(DependencyObject obj, bool value)
        {
            // 为只读附加属性赋值时，SetValue 只能使用对应的 Key 才能赋值
            obj.SetValue(HasTextPropertyKey, value);
        }

        public static readonly DependencyProperty HasTextProperty;

        public static readonly DependencyPropertyKey HasTextPropertyKey =
            DependencyProperty.RegisterAttachedReadOnly(
                name: "HasText",
                propertyType: typeof(bool),
                ownerType: typeof(TextboxHelper),
                defaultMetadata: new PropertyMetadata(false));

        static TextboxHelper()
        {
            HasTextProperty = HasTextPropertyKey.DependencyProperty;
        }

        public static bool GetMoniterTextChange(DependencyObject obj)
        {
            return (bool)obj.GetValue(MoniterTextChangeProperty);
        }

        public static void SetMoniterTextChange(DependencyObject obj, bool value)
        {
            obj.SetValue(MoniterTextChangeProperty, value);
        }

        public static readonly DependencyProperty MoniterTextChangeProperty =
            DependencyProperty.RegisterAttached(
                name: "MoniterTextChange",
                propertyType: typeof(bool),
                ownerType: typeof(TextboxHelper),
                defaultMetadata: new PropertyMetadata(
                    false,
                    new PropertyChangedCallback((o, e) =>
                    {
                        if (o is TextBox textBox)
                        {
                            if ((bool)e.NewValue)
                            {
                                textBox.TextChanged += TextBox_TextChanged;
                                SetHasText(textBox, !string.IsNullOrEmpty(textBox.Text));
                            }
                            else
                            {
                                textBox.TextChanged -= TextBox_TextChanged;
                            }
                        }
                    })));

        private static void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            var textbox = sender as TextBox;
            SetHasText(textbox, !string.IsNullOrEmpty(textbox.Text));
        }


        #endregion
    }
}
